﻿using System;
using System.Diagnostics;
using Ah.Testudin.Engine.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.UI.Elements.Debugging
{
    public class ProfilingScreenElement : ScreenElementBase
    {
        #region Private Member Variables

        private Stopwatch _stopWatch;

        private readonly ITestudinRenderer _renderer;

        private readonly SpriteFont _spriteFont;

        private static ProfilingScreenElement _instance;

        #endregion

        #region Constructors

        public ProfilingScreenElement(
            Vector2 position,
            int width,
            int height,
            SpriteFont spriteFont,
            ITestudinRenderer renderer)
            : base(position, width, height)
        {
            _spriteFont = spriteFont;

            _renderer = renderer;

            _stopWatch = new Stopwatch();
        }

        #endregion

        #region Static Methods

        public static void InitializeSingleton(
            Vector2 position,
            int width,
            int height,
            SpriteFont spriteFont,
            ITestudinRenderer renderer)
        {
            _instance = new ProfilingScreenElement(
                position,
                width,
                height,
                spriteFont,
                renderer);
        }

        public static ProfilingScreenElement GetInstance()
        {
            return _instance;
        }

        #endregion

        #region ScreenElementBase

        public override short ZOrder
        {
            get
            {
                return 0;
            }
        }

        public override bool Visible
        {
            get
            {
                return true;
            }
        }

        public override bool ProcessInput()
        {
            return false;
        }

        public override void Update(GameTime gameTime)
        {
        }

        public void Start()
        {
            if (_stopWatch.IsRunning == false)
            {
                _stopWatch.Reset();

                _stopWatch.Start();
            }
        }

        public void End()
        {
            _stopWatch.Stop();
        }

        public override void Render()
        {
            _renderer.BeginSpriteBatch();

            _renderer.RenderString(
                _spriteFont,
                "Elapsed Time in MS " + (_stopWatch.ElapsedMilliseconds).ToString(),
                _position,
                Color.Yellow);

            _renderer.EndSpriteBatch();
        }

        #endregion
    }
}